package com.example.myapplication.Penson;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.User;
import com.example.myapplication.R;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ModifyPasswordActivity extends AppCompatActivity {

    private EditText oldPassword;
    private EditText newPassword;
    private TextView confirmButton;
    private ImageView block_but;
    private String oldPWD;
    private String newPWD;
    private Request request;
    private OkHttpClient okHttpClient;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    private RequestBody requestBody;
    private Handler handler;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor edit;
    private String password;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        initview();
        img_bloak();
    }

    private void img_bloak() {
        block_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModifyPasswordActivity.this.finish();
            }
        });
    }

    private void initview() {
        oldPassword = findViewById(R.id.oldPassword);
        newPassword = findViewById(R.id.newPassword);
        confirmButton = findViewById(R.id.confirmButton);
        block_but = findViewById(R.id.block_but);
        handler = new Handler();


        //从本地获取密码
        sharedPreferences = ModifyPasswordActivity.this.getSharedPreferences("login", Context.MODE_PRIVATE);
        edit = sharedPreferences.edit();
        password = sharedPreferences.getString("password", "");




        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SendRequest();
            }
        });


    }

    private void SendRequest() {
        oldPWD = oldPassword.getText().toString();
        newPWD = newPassword.getText().toString();
        okHttpClient = new OkHttpClient();

        //判断输入的密码是否和原密码一样
        if(!"".equals(oldPWD.trim()) && !"".equals(password.trim()) && oldPWD.equals(password) && !"".equals(newPWD.trim())){
            //将新密码保存
            edit.putString("password",newPWD);
            edit.apply();
            modifypwd();
        }else {
            Toast.makeText(ModifyPasswordActivity.this,"密码修改失败",Toast.LENGTH_SHORT).show();
            return;
        }
    }

    private void modifypwd() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("newPassword", newPWD);
            jsonObject.put("oldPassword",oldPWD);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        requestBody = RequestBody.create(jsonObject.toString(), JSON);
        request = new Request.Builder().url("http://124.93.196.45:10001/prod-api/api/common/user/resetPwd")
                .addHeader("Authorization", OKHttpTool.TOKEN)
                .put(requestBody)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ModifyPasswordActivity.this.getApplicationContext(), "密码修改成功", Toast.LENGTH_SHORT).show();
                        ModifyPasswordActivity.this.finish();
                    }
                });
            }
        });
    }
}
